package com.yanqu.road.dao.impl.activity.peakseacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCrossUserData;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/13 17:16
 */
public class PeakSeacraftUserDataDaoImpl extends TempDataObjectDao<PeakSeacraftUserData> {
    public static String getPeakSeacraftUserDataSql = "select * from `t_u_activity_peak_seacraft_user` where `activityId`=? and `userId`=?";
    public PeakSeacraftUserData getPeakSeacraftUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getPeakSeacraftUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }


    public static String updatePeakSeacraftUserDataSql = "update `t_u_activity_peak_seacraft_user` set round=?,donationExp=?,silverDonateNum=?,cellInfoList=?,floor=?,gotBigReward=?,worship=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(PeakSeacraftUserData peakSeacraftUserData) {
        peakSeacraftUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getDonationExp()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getSilverDonateNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftUserData.getCellInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getGotBigReward()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getWorship()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftUserData.getUserId()));
        if (getDb().executeNoneQuery(updatePeakSeacraftUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPeakSeacraftUserDataSql = "insert into `t_u_activity_peak_seacraft_user`(activityId,userId,round,donationExp,silverDonateNum,cellInfoList,floor,gotBigReward,worship) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PeakSeacraftUserData peakSeacraftUserData) {
        peakSeacraftUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getDonationExp()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getSilverDonateNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftUserData.getCellInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getGotBigReward()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftUserData.getWorship()));
        if (getDb().executeNoneQuery(insertPeakSeacraftUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PeakSeacraftUserData parseData(ResultSet rs) throws SQLException {
        PeakSeacraftUserData peakSeacraftUserData = new PeakSeacraftUserData();
        peakSeacraftUserData.setActivityId(rs.getInt("activityId"));
        peakSeacraftUserData.setUserId(rs.getLong("userId"));
        peakSeacraftUserData.setRound(rs.getInt("round"));
        peakSeacraftUserData.setDonationExp(rs.getInt("donationExp"));
        peakSeacraftUserData.setSilverDonateNum(rs.getInt("silverDonateNum"));
        peakSeacraftUserData.setCellInfoList(JSON.parseObject(rs.getString("cellInfoList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCellInfo>>() {}));
        peakSeacraftUserData.setFloor(rs.getInt("floor"));
        peakSeacraftUserData.setGotBigReward(rs.getInt("gotBigReward"));
        peakSeacraftUserData.setWorship(rs.getInt("worship"));

        return peakSeacraftUserData;
    }

}